#include <iostream>
#include <strings.h>
#include <unistd.h>
#include "log.hpp"
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>
using namespace std;

int main()
{

    uint16_t port = 8553;
    string ip = "127.0.0.1";
    // 1. 创建socket
    int fd = socket(AF_INET, SOCK_DGRAM, 0);
    if(fd < 0)
    {
        logMessage(FATAL, "client socket fail");
        exit(1);
    }
    logMessage(DEBUG, "client socket success");
    
    // 2.1 填充基本信息
    struct sockaddr_in local;
    bzero(&local, sizeof(local));
    local.sin_family = AF_INET;
    local.sin_port = htons(port);
    local.sin_addr.s_addr = inet_addr(ip.c_str());

    std::string buffer;
    while (true)
    {
        std::cerr << "请输入# ";
        std::getline(std::cin, buffer);
        // 发送消息给server
        sendto(fd, buffer.c_str(), buffer.size(), 0,
               (const struct sockaddr *)&local, sizeof(local)); // 首次调用sendto函数的时候，我们的client会自动bind自己的ip和port
    }

    close(fd);


    return 0;
}